package com.braveman.im.ui.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.braveman.im.R;
import com.braveman.im.bean.GetGroupListBean;
import com.braveman.im.bean.GroupListBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.activity.message.ChatAllActivity;
import com.braveman.im.ui.adapter.MineGroupAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseFragment;
import com.ht.baselibs.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

import ezy.ui.layout.LoadingLayout;
import retrofit2.Call;
import retrofit2.Response;

public class GroupFragment extends BaseFragment {
    private List<GroupListBean> ownerCategoryGroups = new ArrayList<>();
    private List<GroupListBean> adminCategoryGroups = new ArrayList<>();
    private List<GroupListBean> joinedCategoryGroups = new ArrayList<>();
    private List<GroupListBean> allGroups = new ArrayList<>();
    private MineGroupAdapter groupAdapter;

    private RecyclerView mLvActivityListMember;
    private LoadingLayout loadingLayout;


    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected View initContentView() {
        return LayoutInflater.from(mContext).inflate(R.layout.frag_group_fragment, null);
    }

    @Override
    protected void initChildView() {
        groupAdapter = new MineGroupAdapter(R.layout.em_row_group, R.layout.item_group_list_head, allGroups);
        loadingLayout = findViewById(R.id.loading_layout);
        mLvActivityListMember = findViewById(R.id.lv_Activity_list_member);
        mLvActivityListMember.setLayoutManager(new LinearLayoutManager(mContext));
        mLvActivityListMember.setAdapter(groupAdapter);
    }

    @Override
    protected void load() {
        ApiManager.getApi().getGroupJoinList().enqueue(new NetCallBack<GetGroupListBean>() {
            @Override
            protected void onSuccess(Call<GetGroupListBean> call, Response<GetGroupListBean> response) {

                if (response != null) {
                    if (allGroups != null) {
                        allGroups.clear();
                    }
                    GetGroupListBean groupList = response.body();
                    if (groupList != null) {
                        if (groupList.ownerCategory != null) {
                            GroupListBean groupListBean = new GroupListBean(true, "我创建的群", groupList.ownerCategory.count);
                            groupListBean.groupRole = "owner";
                            allGroups.add(groupListBean);
                            ownerCategoryGroups.addAll(groupList.ownerCategory.groupList);
                        }

                        if (groupList.adminCategory != null) {
                            GroupListBean groupListBean = new GroupListBean(true, "我管理的群", groupList.adminCategory.count);
                            groupListBean.groupRole = "admin";
                            allGroups.add(groupListBean);
                            adminCategoryGroups.addAll(groupList.adminCategory.groupList);
                        }

                        if (groupList.joinedCategory != null) {
                            joinedCategoryGroups.addAll(groupList.joinedCategory.groupList);
                            GroupListBean groupListBean = new GroupListBean(true, "我加入的群", groupList.joinedCategory.count);
                            groupListBean.groupRole = "member";
                            allGroups.add(groupListBean);
                            allGroups.addAll(joinedCategoryGroups);

                        }


                    }

                    if (allGroups.size() == 0) {
                        loadingLayout.showEmpty();
                    } else {
                        loadingLayout.showContent();
                    }
                } else {

                    loadingLayout.showError();
                }


                if (groupAdapter != null) {
                    groupAdapter.notifyDataSetChanged();
                }
            }
        });
    }


    @Override
    protected void setChildViewListener() {
        groupAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (allGroups.get(position).isHeader) {
                    if ("owner".equals(allGroups.get(position).groupRole)) {
                        allGroups.clear();
                        GroupListBean groupListBean = new GroupListBean(true, "我创建的群", ownerCategoryGroups.size());
                        groupListBean.groupRole = "owner";
                        allGroups.add(groupListBean);
                        allGroups.addAll(ownerCategoryGroups);
                        GroupListBean groupListBean1 = new GroupListBean(true, "我管理的群", adminCategoryGroups.size());
                        groupListBean1.groupRole = "admin";
                        allGroups.add(groupListBean1);
                        GroupListBean groupListBean2 = new GroupListBean(true, "我加入的群", joinedCategoryGroups.size());
                        groupListBean2.groupRole = "member";
                        allGroups.add(groupListBean2);
                    } else if ("admin".equals(allGroups.get(position).groupRole)) {
                        allGroups.clear();
                        GroupListBean groupListBean = new GroupListBean(true, "我创建的群", ownerCategoryGroups.size());
                        groupListBean.groupRole = "owner";
                        allGroups.add(groupListBean);
                        GroupListBean groupListBean1 = new GroupListBean(true, "我管理的群", adminCategoryGroups.size());
                        groupListBean1.groupRole = "admin";
                        allGroups.add(groupListBean1);
                        allGroups.addAll(adminCategoryGroups);
                        GroupListBean groupListBean2 = new GroupListBean(true, "我加入的群", joinedCategoryGroups.size());
                        groupListBean2.groupRole = "member";
                        allGroups.add(groupListBean2);
                    } else if ("member".equals(allGroups.get(position).groupRole)) {
                        allGroups.clear();
                        GroupListBean groupListBean = new GroupListBean(true, "我创建的群", ownerCategoryGroups.size());
                        groupListBean.groupRole = "owner";
                        allGroups.add(groupListBean);
                        GroupListBean groupListBean1 = new GroupListBean(true, "我管理的群", adminCategoryGroups.size());
                        groupListBean1.groupRole = "admin";
                        allGroups.add(groupListBean1);
                        GroupListBean groupListBean2 = new GroupListBean(true, "我加入的群", joinedCategoryGroups.size());
                        groupListBean2.groupRole = "member";
                        allGroups.add(groupListBean2);
                        allGroups.addAll(joinedCategoryGroups);

                    }

                    if (groupAdapter != null) {
                        groupAdapter.notifyDataSetChanged();
                    }
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("user_id", allGroups.get(position).imGroupId);
                    bundle.putString("groupId", allGroups.get(position).groupId);
                    bundle.putInt("states", 1);
                    bundle.putString("usrName", allGroups.get(position).groupName);
                    ActivityUtils.startActivityAndFinish(mContext, ChatAllActivity.class, bundle);
                }

            }
        });

        loadingLayout.setRetryListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                load();
            }
        });

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
        }
    }

}
